package loan.calc.calculator.impl;



import loan.calc.calculator.Calculator;
import loan.calc.dto.LoanDTO;
import loan.calc.dto.LoanRepaySegmentDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public abstract class AbstractCalculator implements Calculator, ApplicationContextAware {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    protected ApplicationContext applicationContext;

    @Override
    public String calculatorCode() {
        return getClass().getName();
    }

    @Override
    public String calculatorName() {
        String[] beanNames = applicationContext.getBeanNamesForType(getClass());
        if (beanNames.length == 1) {
            return beanNames[0];
        } else {
            return null;
        }
    }

    @Override
    public int getOrder() {
        return calculatorSort();
    }

    @Override
    public void preCalculate(LoanDTO loan) {

    }

    @Override
    public void postCalculate(LoanDTO loan) {

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    protected void printRepaymentSegmentDebugLog(LoanRepaySegmentDTO segment, String message) {
        if (log.isDebugEnabled()) {
            log.debug("计算器[{}]执行日志:还款区段[{}][{}]-[{}]:{}",
                    calculatorName(),
                    segment.getSegmentId(),
                    segment.getSegmentStartDate(),
                    segment.getSegmentMaturityDate(),
                    message);
        }
    }
}
